package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Transactional
public class RoleSreviceImpl implements IRoleService{

    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                roleMapper.insertRelation(role.getId(), roleId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRalation(id);
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRalation(role.getId());
        if (ids!=null &&ids.length>0){
            for (Long roleId : ids) {
                roleMapper.insertRelation(role.getId(), roleId);
            }
        }
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(roleMapper.selectForList(qo));
    }

    @Override
    public List<String> queryRolesByEmployeeId(Long id) {
        return roleMapper.selectRolesByEmployeeId(id);
    }

}
